package com.doyens.user.service;

import com.doyens.common.utils.NumberUtils;
import com.doyens.user.mapper.UserMapper;
import com.doyens.user.pojo.User;
import com.doyens.user.utils.CodecUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.TimeoutUtils;
import org.springframework.stereotype.Service;

import java.security.Key;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.zip.DeflaterOutputStream;

@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private StringRedisTemplate redisTemplate;

    private static final String KEY_PREFIX = "user:verify:";

    public Boolean checkUser(String data,Integer type){
        User user = new User();
        if (type == 1){
            user.setUsername(data);
        }else if (type == 2){
            user.setPhone(data);
        }else{
            return  null;
        }
        return this.userMapper.selectCount(user) == 0;  //true  false
    }

    //发送验证码
    public void sendVerifyCode(String phone) {
        if (StringUtils.isBlank(phone)){
            return;
        }
        //生成验证码
        String code = NumberUtils.generateCode(4);
        //调用qmqptemplate去发送消息
        Map<String,String> map = new HashMap<>();
        map.put("code",code);
        map.put("phone",phone);
        this.amqpTemplate.convertAndSend("doyens.sms.exchange","sms.verify.code",map);
        //存到redis中一份
        this.redisTemplate.opsForValue().set(KEY_PREFIX+phone,code,5,TimeUnit.MINUTES);//5分钟有效
        System.out.println("验证码:==="+code);
    }

    //注册用户
    public void register(User user, String code) {
          //从redis中取出验证码
        String redisCode = redisTemplate.opsForValue().get(KEY_PREFIX + user.getPhone());
        //1.判断验证码
        if (!StringUtils.equals(code,redisCode)){  //验证码不符合
            return;
        }

        //2.生成盐值
        String salt = CodecUtils.generateSalt();
        user.setSalt(salt);
        //3.加盐加密   md5(密码 + 盐值)
        user.setPassword(CodecUtils.md5Hex(user.getPassword(), salt));
        //4.保存到数据库
        user.setId(null);
        user.setCreated(new Date());
        this.userMapper.insertSelective(user);


    }

    //查询用户
    public User queryUser(String username, String password) {
        User recode = new User();
        recode.setUsername(username);
        User users = this.userMapper.selectOne(recode);//从数据库查询出来的数据
        if (users == null){
            return null;
        }
        String pwd = CodecUtils.md5Hex(password, users.getSalt()); //前台输入的密码

        //与数据库中的密码进行比较
        if (StringUtils.equals(pwd,users.getPassword())){
            return users;
        }
        return null;


    }
}
